﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using Utilities;
namespace WordDemo2
{
    class Process1
    {

        Dictionary<string, Dictionary<string, int>> lLeftWord = new Dictionary<string, Dictionary<string, int>>();
        Dictionary<string, Dictionary<string, int>> lRightWord = new Dictionary<string, Dictionary<string, int>>();
        private string removeTag(string input)
        {
            return Regex.Replace(input,@"\[[^\]]+\]","");
        }
        public void process(string path)
        {
            StreamReader sr = new StreamReader(path);
            string line = "";
            while((line=sr.ReadLine())!=null)
            {
                string[] words = line.Split(' ');
                for (int i = 0; i < words.Length; i++)
                {
                    if(words.Length>2)
                    if(i==0)
                    {
                        string w1 = removeTag(words[i]);
                        string w2 = removeTag(words[i+1]);
                        if (lRightWord.ContainsKey(w1))
                        {
                            if (lRightWord[w1].ContainsKey(w2))
                                lRightWord[w1][w2]++;
                            else
                                lRightWord[w1][w2] = 1;
                        }
                        else
                            lRightWord[w1] = new Dictionary<string, int>();

                    }else if(i==words.Length-1)
                    {
                        string w1 = removeTag(words[i]);
                        string w2 = removeTag(words[i - 1]);
                        if (lLeftWord.ContainsKey(w1))
                        {
                            if (lLeftWord[w1].ContainsKey(w2))
                                lLeftWord[w1][w2]++;
                            else
                                lLeftWord[w1][w2] = 1;
                        }
                        else
                            lLeftWord[w1] = new Dictionary<string, int>();
                    }else
                    {
                        string w1 = removeTag(words[i -1]);
                        string w2 = removeTag(words[i]);
                        string w3 = removeTag(words[i + 1]);
                        if (lRightWord.ContainsKey(w2))
                        {
                            if (lRightWord[w2].ContainsKey(w3))
                                lRightWord[w2][w3]++;
                            else
                                lRightWord[w2][w3] = 1;
                        }
                        else
                            lRightWord[w2] = new Dictionary<string, int>();
                        if (lLeftWord.ContainsKey(w2))
                        {
                            if (lLeftWord[w2].ContainsKey(w1))
                                lLeftWord[w2][w1]++;
                            else
                                lLeftWord[w2][w1] = 1;
                        }
                        else
                            lLeftWord[w2] = new Dictionary<string, int>();
                    }
                }
            }
            sr.Close();
        }
        public void write(string path)
        {
            StreamWriter sw = new StreamWriter(path+"/lleftword.txt");
            string line="";
            var items = from pair in lLeftWord
                        orderby pair.Key
                        select pair;
            foreach (var i in items)
            {
                var iitems=from pair in i.Value
                        orderby pair.Key
                        select pair;
                line = i.Key + "|";
                foreach (var ii in iitems)
                {
                    line += ii.Key + ":" + ii.Value + "\t";
                }
                sw.WriteLine(line);
            }
            sw.Close();
            sw = new StreamWriter(path + "/rrightword.txt");
            items = from pair in lRightWord
                        orderby pair.Key
                        select pair;
            foreach (var i in items)
            {
                var iitems = from pair in i.Value
                             orderby pair.Key
                             select pair;
                line = i.Key + "|";
                foreach (var ii in iitems)
                {
                    line += ii.Key + ":" + ii.Value + "\t";
                }
                sw.WriteLine(line);
            }
            sw.Close();
            Debugger.log("xong");
        }
    }
}
